using System;
using Policy = PHSRAG.Insight21.Policy.ICachePolicy;


namespace PHSRAG.Insight21.Policy
{
	/// <summary>
	/// This class is used as a mechanism to cache the search results
	/// from one UI page/form at a time.  As the user navigates across pages/forms,
	/// the previous search results will be uncached while the latest results are cached.
	/// </summary>
   public class ResultsCache
   {
      /// <summary>
      /// Key identifying the search results.
      /// </summary>
      public const string ResultsCacheKey = "ResultCacheKey_E0BA9BF3-58C7-4649-88BD-4CF0766FA0F7";

      private string key;
      private object cachedObject;

      /// <summary>
      /// Key identifying the object to be cached.
      /// </summary>
      public string Key
      {
         get
         {
            return key;
         }
         set
         {
            key = value;
         }
      }

      /// <summary>
      /// Actual object to be cached.
      /// </summary>
      public object Object
      {
         get
         {
            return cachedObject;
         }
         set
         {
            cachedObject = value;
         }
      }


      /// <summary>
      /// Gets the cached object based on the passed key from the Results Cache.
      /// If a different object is cached, it is uncached and null is returned.
      /// </summary>
      /// <param name="cachePolicy">Cache policy being used for caching</param>
      /// <param name="key">Key of the object to cache.</param>
      /// <returns>Cached object</returns>
      public static object GetObject(Policy.ICachePolicy cachePolicy, string key)
      {
         ResultsCache resultCache =
            cachePolicy.GetCachedObject(ResultsCacheKey) as ResultsCache;

         if ( null != resultCache )
         {
            if ( resultCache.Key == key )
            {
               return resultCache.Object;
            }
            else
            {
               cachePolicy.UnCache(ResultsCacheKey);
            }
         }

         return null;
      }

      /// <summary>
      /// Sets the passed object in the Results Cache.  If there is already an
      /// object in the Results Cache, it is uncached.
      /// </summary>
      /// <param name="cachePolicy">Cache policy being used for caching</param>
      /// <param name="key">Key of the object to cache.</param>
      /// <param name="obj">Object to be cached</param>
      public static void SetObject(Policy.ICachePolicy cachePolicy, string key, object obj)
      {
         ResultsCache resultsCache = new ResultsCache();

         resultsCache.Key = key;
         resultsCache.Object = obj;

         cachePolicy.Cache(ResultsCacheKey, resultsCache);
      }

      /// <summary>
      /// Removes the Results Cache from the passed cache policy.
      /// </summary>
      /// <param name="cachePolicy">Cache policy being used for caching</param>
      public static void PurgeObject(Policy.ICachePolicy cachePolicy)
      {
         cachePolicy.UnCache(ResultsCacheKey);
      }

      /// <summary>
      /// Determines if the passed key is in the results cache.
      /// </summary>
      /// <param name="cachePolicy">Cache policy being used for caching</param>
      /// <param name="key">Key of the object to verify if cached.</param>
      /// <returns></returns>
      public static bool IsCached(Policy.ICachePolicy cachePolicy, string key)
      {
         ResultsCache resultCache =
            cachePolicy.GetCachedObject(ResultsCacheKey) as ResultsCache;

         if ( null == resultCache )
         {
            return false;
         }
         else
         {
            return ( resultCache.Key == key ? true : false );
         }
      }
   }
}
